package com.bing.Impl;

import com.bing.exception.WeiBoException;
import com.bing.util.Context;
import com.bing.util.TaoBao;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * Created by Administrator on 2014/6/20.
 */

@Component
public class Login extends AbstrcatStep {
    private static final Logger logger = LoggerFactory.getLogger(Login.class);

    private String url = "http://weibo.com/login.php";

    private String userName;

    private String passWord;

    @Override
    public void preStart(Context context) {
        super.preStart(context);
        logger.info("开始Login...........");
        userName = context.getUserName();
        passWord = context.getPassWord();
    }

    @Override
    public void end(Context context) {
        super.end(context);
        logger.info("结束Login...........");
    }

    @Value("#{sina['step.login.process']}")
    public void setProcess(Boolean process) {
        super.setProcess(process);
    }

    /**
     * 登陆淘大客,获取淘大客资料
     */
    @Override
    public void exec(Context context) {
        for (int i = 0; i <= 3; i++) {
            try {
                logger.info("第{}次尝试登录.", i);
                driver.get(url);
                TaoBao.pause();
                if (context.getTaoListener() != null) {
                    context.getTaoListener().setStatus("登陆: 新浪");
                }
                driver.findElement(By.cssSelector("div.W_login_form input[name='username']")).sendKeys(userName);
                driver.findElement(By.cssSelector("div.W_login_form input[name='password']")).sendKeys(passWord);
                TaoBao.pause();
                driver.findElement(By.cssSelector("div.W_login_form div.login_btn a.W_btn_g")).click();
                logger.info("登入成工........");
                return;
            } catch (Exception e) {
                if (i == 3) {
                    logger.info("登入失败.");
                    throw new WeiBoException(Context.LOGIN_FAILED, e);
                }
            }
        }
    }

    @Autowired
    @Qualifier("getPersonStep")
    public void setNextStep(AbstrcatStep nextStep) {
        super.setNextStep(nextStep);
    }

}
